//
//  TuijianViewController.m
//  test1
//
//  Created by chen on 16/10/29.
//  Copyright © 2016年 chen. All rights reserved.
//
#import "TuijianViewController.h"
//#import "HMSlideView.h"
#import "Masonry.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "SDCycleScrollView.h"
#import "HeaderTableViewCell.h"
#import "MiddleTableViewCell.h"
#import "BottomTableViewCell.h"
#import "RecommendsModel.h"
#import "UIImageView+WebCache.h"
#import "MyPushViewController.h"
#import "ModelViewController.h"
#import "ModelViewController2.h"
#import "ButtonViewController.h"


#define NUM 30

@interface TuijianViewController () <UITableViewDelegate,UITableViewDataSource , HeaderTableViewCellDelegate>

@property(nonatomic,strong)NSArray<NSURL*>*arr;

//推荐界面 的 cell数据
@property(nonatomic,strong)NSArray<RecommendsModel *>*recommendsModel;

@property(nonatomic,strong)NSURL *logoUrlTopic;

@property(nonatomic,strong)NSData *data;

@end

@implementation TuijianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //保存URL的数组
    self.arr=@[@"http://wimg.huodongxing.com/Content/app/appom/622492454354615.jpg",
               @"http://wimg.huodongxing.com/Content/app/appom/882472505355506.jpg",
               @"http://wimg.huodongxing.com/Content/app/appom/882478460206044.jpg",
               @"http://wimg.huodongxing.com/Content/app/appom/152482432954477.jpeg",
               @"http://wimg.huodongxing.com/Content/app/appom/512474436408896.jpg",
               @"http://wimg.huodongxing.com/Content/app/appom/462463719205212.jpg"
               ];
    //初始化轮播器
//    SDCycleScrollView *view=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 120) imageNamesGroup:self.arr];
//    [self.view addSubview:view];
    
    self.data = [NSData data];
    
    [self loadData];
    
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    
    UINib *nib1 = [UINib nibWithNibName:@"HeaderTableViewCell" bundle:nil];
    [self.tableView registerNib:nib1 forCellReuseIdentifier:@"cell1"];
    UINib *nib2 = [UINib nibWithNibName:@"MiddleTableViewCell" bundle:nil];
    [self.tableView registerNib:nib2 forCellReuseIdentifier:@"cell2"];
    UINib *nib3 = [UINib nibWithNibName:@"BottomTableViewCell" bundle:nil];
    [self.tableView registerNib:nib3 forCellReuseIdentifier:@"cell3"];
    
    //设置导航栏
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"tabbar_discover"] style:UIBarButtonItemStyleDone target:self action:@selector(presentView2)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"广州" style:UIBarButtonItemStylePlain target:self action:@selector(presentView1)];
    self.navigationItem.title = @"活动行";
    
}

#pragma mark --拖拽刷新
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    //下拉刷新
    if (self.tableView.contentOffset.y <0) {
        [self loadData];
    }
    else {                  //上拉刷新
        
    }
    
}

- (void)presentView1 {
    ModelViewController *modelV = [[ModelViewController alloc]init];
    modelV.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    [self presentViewController:modelV animated:YES completion:nil];
}

- (void)presentView2 {
    ModelViewController2 *modelV = [[ModelViewController2 alloc]init];
    modelV.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    [self presentViewController:modelV animated:YES completion:nil];
}

- (void)loadData {
    
    NSURL *url = [NSURL URLWithString:@"http://api.huodongxing.com/v3/search/home_data?city="];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        _data = [NSData dataWithContentsOfURL:url];
        if (_data ==nil) {
            return ;
        }
        
        //获得json中的词典
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:nil];
        //获得字典中key为result 的字典
        NSDictionary *dictResult = dict[@"result"];
        
        //获得banners
        NSArray *arrBanners = dictResult[@"banners"];
        //获得combos
        NSArray *arrCombos = dictResult[@"combos"];
        //获得topics
        NSArray *arrTopics = dictResult[@"topics"];
        //获得recommends
        NSArray *arrRecommends = dictResult[@"recommends"];
        //获得poporgs
        NSArray *arrPoporgs = dictResult[@"poporgs"];
        
        //topic
        NSDictionary *topicDict = arrTopics[0];
        NSURL *urlTopic = [NSURL URLWithString:topicDict[@"logo"]];
        self.logoUrlTopic = urlTopic;
        
        //recommend
        NSMutableArray *arrMRecommends = [NSMutableArray array];
        for (NSDictionary *dict in arrRecommends) {
            RecommendsModel *recommendsModel = [RecommendsModel recommendsModelWithDict:dict];
            [arrMRecommends addObject:recommendsModel];
        }
        self.recommendsModel = arrMRecommends.copy;
        
        
//        [self loadData];
        
        dispatch_async( dispatch_get_main_queue() , ^{
            [self.tableView reloadData];
        });
        
    });
}

#pragma mark ---HeaderTableViewCellDelegate

-(void)sendSender:(UIButton *)sender {
    
    NSURL *url = [NSURL URLWithString:@"http://api.huodongxing.com/v3/search/query_events?filter=3&date=t8&start=2016-10-30+00%3A00%3A00&end=2016-10-30+23%3A59%3A59&city=&pi=2&ps=20&onlyunexpired=true&onlyunfull=false"];
    ButtonViewController *vc = [[ButtonViewController alloc]initWithUrl:url];
    vc.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}


#pragma mark ---数据源和协议方法
//设置组间距
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return self.recommendsModel.count;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section ==0) {
        
        HeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.delegate = self;
        cell.arr = self.arr.copy;
        return cell;
    }else if (indexPath.section == 1){
        MiddleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
//        [cell.imageView sd_setImageWithURL:self.logoUrlTopic];
        
        return cell;
    }
    
    BottomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
    
    cell.model = self.recommendsModel[indexPath.row];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section != 0) {
        
        MyPushViewController *myV = [[MyPushViewController alloc]init];
        myV.urlString = self.recommendsModel[indexPath.row].url;
        myV.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:myV animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
